<?php

/**
 * @author Djordje Zeljic
 * Date: May 27, 2010 3:46:07 PM
 */
class customers_m extends Model {

    function customers_m () {
        parent::Model();
    }// construct of customers_m

    function create_for($cdata,$adata) {
        $this->db->insert('customers',$cdata);

        $adata['customerId'] = $this->db->insert_id();
        $this->db->insert('accounts',$adata);
    }

    function create() {

    }

    function read($id, $cascaderead = FALSE) {
        if($cascaderead == TRUE) {
            return $this->db->select('*')->from('customers')
                    ->join('accounts','accounts.customerid = customers.id','left')
                    ->join('countries','countries.ccode = customers.countrycode','left')
                    ->where('id', $id)
                    ->get()
                    ->row_array();
        }else {
            return $this->db->where('id',$id)->get('customers')->row_array();
        }
    }

    function readAll($cascaderead = FALSE){
        if($cascaderead == TRUE){
            return $this->db->select('*')
                    ->from('customers')
                    ->join('accounts','accounts.customerid = customers.id','left')
                    ->join('countries','countries.ccode = customers.countrycode','left')
                    ->order_by('id','desc')
                    ->get()
                    ->result_array();
        }else{
            return $this->db->select('*')
                    ->from('customers')
                    ->order_by('id','desc')
                    ->get()
                    ->result_array();
        }
    }

    function update() {

    }

    function delete() {

    }

}
?>